/***************************************************************************
      File Name:  CallerID.cs
   File Created:  Wednesday, June 13th 2007
      Copyright:  (C) 2007 by Darko Matesic
          Email:  darko.matesic@yahoo.com
 ***************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace CallerID
{
    /// <summary>
    /// Singleton class for logging data.
    /// </summary>
    public class LogWindow: Form
    {
        private static readonly LogWindow m_Instance = new LogWindow();
        public static LogWindow Instance { get { return LogWindow.m_Instance; } }

        /// <summary>
        /// Appends text to log with optional timestamp.
        /// </summary>
        public static void Append(string text, bool timestamp)
        {
            m_Instance.LogAppend(text, timestamp);
        }
        /// <summary>
        /// Appends text to log.
        /// </summary>
        public static void Append(string text)
        {
            m_Instance.LogAppend(text);
        }

        /// <summary>
        /// Appends text to log with optional timestamp and advances to new line.
        /// </summary>
        public static void AppendLine(string text, bool timestamp)
        {
            m_Instance.LogAppendLine(text, timestamp);
        }
        /// <summary>
        /// Appends text to log and advances to new line.
        /// </summary>
        public static void AppendLine(string text)
        {
            m_Instance.LogAppendLine(text);
        }

        /// <summary>
        /// Clears log.
        /// </summary>
        public static void Clear()
        {
            m_Instance.LogClear();
        }

        /// <summary>
        /// Gets number of lines in log.
        /// </summary>
        public static int Lines
        {
            get { return m_Instance.LogLines; }
        }



        #region Windows Form Designer generated code

        private TextBox textLog;

        private void InitializeComponent()
        {
            this.textLog = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // textLog
            // 
            this.textLog.BackColor = System.Drawing.SystemColors.Window;
            this.textLog.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textLog.Location = new System.Drawing.Point(0, 0);
            this.textLog.Multiline = true;
            this.textLog.Name = "textLog";
            this.textLog.ReadOnly = true;
            this.textLog.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.textLog.Size = new System.Drawing.Size(301, 182);
            this.textLog.TabIndex = 0;
            // 
            // LogWindow
            // 
            this.ClientSize = new System.Drawing.Size(301, 182);
            this.Controls.Add(this.textLog);
            this.Name = "LogWindow";
            this.Text = "Log";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private LogWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Appends text to log with optional timestamp.
        /// </summary>
        public void LogAppend(string text, bool timestamp)
        {
            if(this.textLog.InvokeRequired) {
                AppendCallback d = new AppendCallback(LogAppend);
                this.Invoke(d, new object[] { text, timestamp });
            } else {
                if(timestamp) {
                    string ts = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    if(this.textLog != null && this.textLog.IsDisposed == false) {
                        this.textLog.AppendText(ts + " : " + text);
                    }
                } else {
                    if(this.textLog != null && this.textLog.IsDisposed == false) {
                        this.textLog.AppendText(text);
                    }
                }
            }
        }
        /// <summary>
        /// Appends text to log.
        /// </summary>
        public void LogAppend(string text)
        {
            LogAppend(text, false);
        }

        /// <summary>
        /// Appends text to log with optional timestamp and advances to new line.
        /// </summary>
        public void LogAppendLine(string text, bool timestamp)
        {
            LogAppend(text + Environment.NewLine, timestamp);
        }
        /// <summary>
        /// Appends text to log and advances to new line.
        /// </summary>
        public void LogAppendLine(string text)
        {
            LogAppendLine(text, false);
        }

        /// <summary>
        /// Clears log.
        /// </summary>
        public void LogClear()
        {
            if(this.textLog.InvokeRequired) {
                ClearCallback d = new ClearCallback(LogClear);
                this.Invoke(d, null);
            } else {
                if(this.textLog != null && this.textLog.IsDisposed == false) {
                    this.textLog.Clear();
                }
            }
        }

        /// <summary>
        /// Returns number of lines in log.
        /// </summary>
        private int GetLogLines()
        {
            if(this.textLog.InvokeRequired) {
                LogLinesCallback d = new LogLinesCallback(GetLogLines);
                return (int)this.Invoke(d, null);
            } else {
                return (this.textLog.Lines != null && this.textLog.IsDisposed == false ? this.textLog.Lines.Length : 0);
            }
        }

        /// <summary>
        /// Gets number of lines in log.
        /// </summary>
        public int LogLines
        {
            get { return GetLogLines(); }
        }

        
        private delegate void AppendCallback(string text, bool timestamp);
        private delegate void ClearCallback();
        private delegate int LogLinesCallback();

    }
}
